package panda.li.navigation.activity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.google.gson.internal.LinkedTreeMap;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import panda.li.navigation.R;
import panda.li.navigation.adapter.BusLineAdapter;
import panda.li.navigation.base.ActivityCollector;
import panda.li.navigation.base.BaseActivity;
import panda.li.navigation.base.MyApplication;
import panda.li.navigation.callback.BaseCallBack;
import panda.li.navigation.callback.BusInfoCallBack;
import panda.li.navigation.constant.Constant;
import panda.li.navigation.entity.BusInfo;
import panda.li.navigation.entity.BusLine;
import panda.li.navigation.entity.ReaderInfo;
import panda.li.navigation.utils.LogUtil;
import panda.li.navigation.utils.MapToObj;
import panda.li.navigation.utils.Result;
import panda.li.navigation.utils.ToastUtil;

public class LoginActivity extends BaseActivity {


    @Bind(R.id.btn_login)
    Button btn_login;
    @Bind(R.id.btn_back)
    Button btn_back;
    @Bind(R.id.et_username)
    EditText et_username;
    @Bind(R.id.et_passwd)
    EditText et_passwd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

    }




    @OnClick({R.id.btn_login, R.id.btn_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                doLogin();
                break;
            case R.id.btn_back:
                ActivityCollector.removeActivity(this);
                break;
        }

    }

    private void doLogin() {

        String username = et_username.getText().toString().trim();
        String passwd = et_passwd.getText().toString().trim();


        OkHttpUtils.post().url(Constant.USER_LOGIN)
            .addParams("username", username)
            .addParams("password", passwd)
            .build()
            .execute(new BaseCallBack<Result>() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    ToastUtil.show(MyApplication.getInstance(), e.getMessage());
                }

                @Override
                public void onSuccess(Result response, int id) {
                    String busStationName = "";
                    if (response != null) {
                        com.google.gson.internal.LinkedTreeMap map = (LinkedTreeMap) response.getData();
                        try {
                            String username = (String) map.get("username");
                            MyApplication.setUsername(username);




                            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                            startActivity(intent);


                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }


            });

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }
}
